<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="icon" th:href="@{/img/logo.ico}" href="../static/img/logo.ico" type="images/x-ico"/>

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="../static/css/bootstrap.min.css" th:href="@{/css/bootstrap.min.css}">
    <title>课程列表</title>
    <link rel="stylesheet" th:href="@{/fontawesome/css/font-awesome.min.css}"
          href="../../static/fontawesome/css/font-awesome.min.css">

    <link rel="stylesheet"
          href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,500;0,600;0,700;1,400&display=swap">

    <!-- Fontawesome CSS -->
    <link rel="stylesheet" href="../static/fontawesome/css/fontawesome.min.css"
          th:href="@{/fontawesome/css/fontawesome.min.css}">
    <link rel="stylesheet" href="../static/fontawesome/css/all.min.css" th:href="@{/fontawesome/css/all.min.css}">

    <!-- Main CSS -->
    <link rel="stylesheet" href="../static/css/back_index.css" th:href="@{/css/back_index.css}">
</head>
<body>
<div class="main-wrapper">
    <div th:replace="commons/bar::#studentmenubar(activeUri1='选报课程')"></div>
    <div class="page-wrapper">
        <div class="content container-fluid">
            <ul class="nav nav-tabs" id="myTab" role="tablist">
                <li class="nav-item" role="presentation">
                    <a class="nav-link active" id="first-tab" data-toggle="tab" href="#first" onclick="TableInit(0)"
                       role="tab"
                       aria-controls="first"
                       aria-selected="true" style="color: black;">专业选修课</a>
                </li>
                <li class="nav-item dropdown" role="presentation">
                    <a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button"
                       aria-haspopup="true"
                       aria-expanded="false" style="color: black;">公共选修课</a>
                    <div class="dropdown-menu" style="min-width:200px">
                        <a class="nav-link" id="second-tab1" data-toggle="tab" href="#second1" onclick="TableInit(1)"
                           role="tab"
                           aria-controls="profile"
                           aria-selected="false">高新技术类</a>
                        <a class="nav-link" id="second-tab2" data-toggle="tab" href="#second2" onclick="TableInit(2)"
                           role="tab"
                           aria-controls="profile"
                           aria-selected="false">跨学科领域类</a>
                        <a class="nav-link" id="second-tab3" data-toggle="tab" href="#second3" onclick="TableInit(3)"
                           role="tab"
                           aria-controls="profile"
                           aria-selected="false">数学与自然科学类</a>
                        <a class="nav-link" id="second-tab4" data-toggle="tab" href="#second4" onclick="TableInit(4)"
                           role="tab"
                           aria-controls="profile"
                           aria-selected="false">人文与社会科学类</a>
                        <a class="nav-link" id="second-tab5" data-toggle="tab" href="#second5" onclick="TableInit(5)"
                           role="tab"
                           aria-controls="profile"
                           aria-selected="false">中华文明与外国文化类</a>
                        <a class="nav-link" id="second-tab6" data-toggle="tab" href="#second6" onclick="TableInit(6)"
                           role="tab"
                           aria-controls="profile"
                           aria-selected="false">礼仪修养与语言表达类</a>
                    </div>
                </li>
            </ul>
            <div class="tab-content" id="myTabContent">
                <!-- 专业选修课-->
                <div class="tab-pane fade active show" id="first" role="tabpanel" aria-labelledby="first-tab">
                    <p class="mb-2 text-muted" style="font-size: smaller">
                        专业选修课程由学校根据本校学生的知识水平能力和兴趣确定，为学校认为部分学生有能力和兴趣完成某一专业课程，又不是必需作为所有学生知识成长一部分的课程。
                    </p>
                    <div id="mytable0"></div>
                </div>
                <!-- 6个公共选修课-->
                <div class="tab-pane fade" th:each="i:${#numbers.sequence(1,6)}" th:id="'second' + ${i}" role="tabpanel"
                     th:aria-labelledby="'second-tab' + ${i}">
                    <p class="mb-2 text-muted" style="font-size: smaller">
                        区别于专业课，公共选修课是自选的，可选可不选，只需要按照自己的想法选择即可。
                    </p>
                    <div th:id="'mytable' + ${i}"></div>
                </div>
            </div>
            <div class="modal fade" id="viewCourseModal" tabindex="-1" role="dialog" aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h4 class="modal-title">课程详情</h4>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                        <div class="modal-body">
                            <table align="center" class="table table-bordered">
                                <tr>
                                    <td>名称</td>
                                    <td>
                                        <span id="courseName"></span>
                                    </td>
                                </tr>
                                <tr>
                                    <td>ID</td>
                                    <td>
                                        <span id="courseId"></span>
                                    </td>
                                </tr>
                                <tr>
                                    <td>类型</td>
                                    <td>
                                        <span id="courseType"></span>
                                    </td>
                                </tr>
                                <tr>
                                    <td>学时</td>
                                    <td>

                                        <span id="time"></span>
                                    </td>
                                </tr>
                                <tr>
                                    <td>学分</td>
                                    <td>
                                        <span id="credits"></span>
                                    </td>
                                </tr>
                                <tr>
                                    <td>开始选报时间</td>
                                    <td>
                                        <span id="beginDate"></span>
                                    </td>
                                </tr>
                                <tr>
                                    <td>结束选报时间</td>
                                    <td>
                                        <span id="endDate"></span>
                                    </td>
                                </tr>
                                <tr>
                                    <td>描述</td>
                                    <td>
                                        <span id="info"></span>
                                    </td>
                                </tr>
                            </table>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-primary" id="submitSelect">选报</button>
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">我已了解</button>
                        </div>
                    </div>
                </div>
            </div>
            <!-- Footer -->
            <footer>
                <p>2020 &copy; 软件学院学生信息管理系统 by 郑焙元</p>
            </footer>
            <!-- /Footer -->
        </div>
    </div>
</div>
<script src="../static/js/jquery-3.5.1.min.js" th:src="@{/js/jquery-3.5.1.min.js}"></script>
<script>

    $(function () {
        TableInit(0);
        var buttonGroup = new ButtonGroup();
        buttonGroup.init();
    });

    var TableInit = function (i) {
        for (var t = 0; t <= 6; t++) {
            var mytable = "#mytable" + t;
            if (t === i) {
                $(mytable).html('<table class="table table-striped table-bordered" id="datas">' +
                    '<thead>\n' +
                    '                        <tr>\n' +
                    '                            <th scope="col">名称</th>\n' +
                    '                            <th scope="col">状态</th>\n' +
                    '                            <th scope="col">学时</th>\n' +
                    '                            <th scope="col">学分</th>\n' +
                    '                            <th scope="col">操作</th>\n' +
                    '                        </tr>\n' +
                    '                        </thead>\n' +
                    '                        <tbody>\n' +
                    '                        <tr id="template">\n' +
                    '                            <td class="coursename"></td>\n' +
                    '                            <td class="status"></td>\n' +
                    '                            <td class="time"></td>\n' +
                    '                            <td class="credits"></td>\n' +
                    '                            <td class="operation"></td>\n' +
                    '                        </tr>\n' +
                    '                        </tbody>' +
                    '</table>');
            } else {
                $(mytable).html("");
            }
        }
        sendAjax('/student/api/course/type/' + i, 'get', {
            coursetype: i,
        }, function (res) {
            var today = new Date();
            $.each(res.data, function (i, course) {
                var row = $("#template").clone();
                row.find(".coursename").text(course.coursename);
                var status;
                if (today > new Date(course.begindate)) {
                    if (today < new Date(course.enddate)) {
                        status = "进行中";
                    } else {
                        status = "已结束";
                    }
                } else {
                    status = "未开始";
                }
                row.find(".status").text(status);
                row.find(".time").text(course.time);
                row.find(".credits").text(course.credits);
                let html = '<button type="button" class="btn btn-info btn-sm" data-toggle="modal" ' +
                    'data-target="#viewCourseModal" onclick="viewCourse(\'' + course.courseid + '\')">详情</button>';
                row.find(".operation").html(html);
                var dataid = 1;
                row.appendTo("#datas");//添加到模板的容器中
            });
            $("#template:first").remove();
        }, '');
    }


    function viewCourse(courseid) {
        sendAjax('/student/api/course/' + courseid, 'get', {}, function (res) {
            $("#courseName").text(res.data.coursename);
            $("#courseId").text(res.data.courseid);
            $("#courseType").text(switchCoursetype(res.data.coursetype));
            $("#time").text(res.data.time);
            $("#credits").text(res.data.credits);
            $("#beginDate").text(res.data.begindate.substring(0, 10));
            $("#endDate").text(res.data.enddate.substring(0, 10));
            $("#info").text(res.data.info);
        }, '');

        sendAjax("/student/api/course", "get", {}, function (res) {
            var b = false;
            $.each(res.data, function (i, course) {
                if (course.courseid === courseid) {
                    b = true;
                }
            })
            if (b) {
                $("#submitSelect").prop("disabled", "disabled");
            } else {
                $("#submitSelect").removeAttr("disabled");
            }
        }, "");
    }

    var ButtonGroup = function () {
        var object = {};

        object.init = function () {
            $("#submitSelect").click(function () {
                if (confirm("确认选报该课程？")) {
                    sendAjax("/student/api/course/" + $("#courseId").text(), "post", {}, function () {
                        alert("选报成功");
                        $("#viewCourseModal").modal('hide');
                    }, "#submitSelect");
                }
            })
        };

        return object;
    };

</script>
<script src="../static/js/popper.min.js" th:src="@{/js/popper.min.js}"></script>
<script src="../static/js/bootstrap.min.js" th:src="@{/js/bootstrap.min.js}"></script>

<!-- Slimscroll JS -->
<script src="../static/js/jquery.slimscroll.min.js" th:src="@{/js/jquery.slimscroll.min.js}"></script>

<!-- Custom JS -->
<script src="../static/js/script.js" th:src="@{/js/script.js}"></script>

</body>
</html>
